﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Reflection;
using System.IO;

namespace Launcher
{
    class Program
    {
        static void Main(string[] args)
        {
            RestartMyBoard();            
        }

        static void RestartMyBoard()
        {
            Process[] processes = System.Diagnostics.Process.GetProcessesByName("myBoard.App");
            Assembly assembly = Assembly.GetExecutingAssembly();
            string exePath = Path.Combine(Path.GetDirectoryName(assembly.Location), "myBoard.App.exe");
            if (processes == null || processes.Length == 0)
            {
                Process.Start(exePath);
            }
            else
            {
                Process p = processes[0];
                AutoResetEvent autoResetEvent = new AutoResetEvent(false);
                p.Exited += (sender, e) =>
                    {
                        Process.Start(exePath);
                        autoResetEvent.Set();
                    };
                p.EnableRaisingEvents = true;
                p.Kill();
                autoResetEvent.WaitOne();
            }
        }
    }
}
